package com.pan.crawler.auto2.action;

import com.pan.download5.parse.entity.auto2.Auto2InfoNext;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NextPage {


    private XPathFind xPathFind;
    {
        xPathFind= new XPathFind();
    }
    private  String xpath;
    private String click;
    private String regex;

    private String symbol;

    private boolean isNext = true;


    private ConditionType conditionType;
    private enum ConditionType{URL,STR}

    private Map<ConditionType, Function<EdgeDriver, String>> conditionMap = new HashMap<>();
    //url
    private Function<EdgeDriver, String> urlAction = EdgeDriver::getCurrentUrl;


    public NextPage(Auto2InfoNext auto2InfoNext) {
        if (auto2InfoNext == null){
            isNext = false;
            return;
        }
        xpath = auto2InfoNext.getXpath();
        if (xpath == null) {
            isNext = false;
            return;
        }
        click = auto2InfoNext.getClick();
        if (click == null) {click = xpath;}
        regex = auto2InfoNext.getRegex();


        Pattern strPattern = Pattern.compile("^\\s*([!=]=)\\s*\'([\\s\\S]+)\'\\s*$");
        Pattern pagePattern = Pattern.compile("^\\s*([!=]=)\\s*Page.url\\s*$");
        try {
            Matcher strMatcher = strPattern.matcher(auto2InfoNext.getCondition());
            Matcher pageMatcher = strPattern.matcher(auto2InfoNext.getCondition());
            //str
            if (strMatcher.find()){
                symbol = strMatcher.group(1);
                String condition = strMatcher.group(2);
                conditionType = ConditionType.STR;
                conditionMap.put(conditionType, d->condition);
            }
            //page.url
            else if (pageMatcher.find()){
                symbol = strMatcher.group(1);
                conditionType = ConditionType.URL;
                conditionMap.put(conditionType, EdgeDriver::getCurrentUrl);
            }
        }catch (Exception e){
            throw new RuntimeException("condition ERROR: "+auto2InfoNext.getCondition(),e);
        }
    }




    /**
     *
     * @param edgeDriver
     * @return
     * true 翻下一页
     * false 没有下一页
     */
    public boolean next(EdgeDriver edgeDriver){
        if (!isNext) return false;
        String target = xPathFind.find(edgeDriver, xpath);
        String condition = conditionMap.get(this.conditionType).apply(edgeDriver);
        if (target == null) return false;
        if (this.symbol.equals("==")){
            if (condition.equals(target)){
                click(edgeDriver);
                return true;
            }else {
                return false;
            }
        }else if (this.symbol.equals("!=")){
            if (condition.equals(target)){
                return false;
            }else {
                click(edgeDriver);
                return true;
            }
        }else {
            return false;
        }
    }
    private void click(EdgeDriver edgeDriver){
        WebElement element = xPathFind.findElement(edgeDriver, click);
        element.click();
    }

}
